package vistas;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import org.hibernate.PersistentObjectException;

import controller.ControllerPrincipal;

public class LoginManager extends BaseJFrame {
	
	private static final long serialVersionUID = -6056370520374539080L;

	
	private String intro = "Para ingresar al sistema debe ingresar el numero de sucursal de la ODV";
	
	
	private JLabel lblUsuario = new JLabel("Nro: Sucursal:");
	private JTextField usuario = new JTextField(10);
	
	
	private JButton login = new JButton("Login");
	private JButton cancelar = new JButton("Cancelar");
	
	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				LoginManager inst = new LoginManager();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public LoginManager(){
		GridBagConstraints constraints = new GridBagConstraints();
		getContentPane().setLayout(new BorderLayout());
		setResizable(false);
		setTitle("Autenticacion");
		
		JPanel north = new JPanel();
		JPanel center = new JPanel();
		JPanel south = new JPanel();
		
		getContentPane().add(north, BorderLayout.NORTH);
		getContentPane().add(center, BorderLayout.CENTER);
		getContentPane().add(south, BorderLayout.SOUTH);
		
		north.setLayout(new BoxLayout(north, BoxLayout.PAGE_AXIS));
		
		center.setLayout(new GridBagLayout());
		constraints.insets = new Insets(3,3,3,3);
		constraints.gridx = 0;
		constraints.gridy = 0;
		
		{
			constraints.gridx = 0;
	        constraints.weightx = 0;
	        constraints.weighty = 0;
	        constraints.anchor = GridBagConstraints.LINE_END;
	        center.add(lblUsuario, constraints);
	        constraints.gridx = 1;
	        constraints.fill = GridBagConstraints.HORIZONTAL;
	        center.add(usuario, constraints);
	        lblUsuario.setLabelFor(usuario);
	        usuario.setToolTipText("Nro. Sucursal validos: \n" +1);
	        constraints.gridy++;
		}
		{
			constraints.gridx = 0;
	        constraints.weightx = 0;
	        constraints.weighty = 0;
	        constraints.anchor = GridBagConstraints.LINE_END;
	        constraints.gridx = 1;
	        constraints.fill = GridBagConstraints.HORIZONTAL;
	        constraints.gridy++;
		}
		
		south.setLayout(new BoxLayout(south, BoxLayout.LINE_AXIS));
        south.add(Box.createHorizontalGlue()); 
        south.add(Box.createRigidArea(new Dimension(10, 0))); 
        south.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        south.add(login);
        south.add(cancelar);
        
        
        login.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				usuario.getText();
				try{
				if(!ControllerPrincipal.getInstancia().validarODV(usuario.getText())){
					JOptionPane.showMessageDialog(new JFrame(), "ODV Invalida");
					usuario.setText("");
				}else{
					ControllerPrincipal.setOdv(new Integer(usuario.getText()));
					MenuPrincipal mp = new MenuPrincipal();
					mp.setVisible(true);
					dispose();
				}
				}catch (Exception e) {
					handleException(e);
				}
			}
			
			});
        
        cancelar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
        
        validate();
        pack();
	}

	protected void handleException(Exception ex) {
		try{
			ex.printStackTrace();
			throw ex;
		}catch(RemoteException e){
			JOptionPane.showMessageDialog(new JFrame(), "Ocurrio un problema al intentar conectar con la CPR.");
			
		}catch(PersistentObjectException e){
			JOptionPane.showMessageDialog(new JFrame(), "Ocurrio un error en el servidor.");
		}catch(Exception e){
			JOptionPane.showMessageDialog(new JFrame(), e.getMessage());
		}
	}
}
